<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $user_code
 * @property string $username
 * @property string $password
 * @property string $roles
 * @property string $user_fname
 * @property string $user_lname
 * @property string $user_mname
 * @property string $user_gender
 * @property string $user_birthdate
 * @property string $user_marital_status
 * @property string $user_address1
 * @property string $user_address2
 * @property string $user_mobile
 * @property string $user_landline
 * @property string $user_email
 * @property string $user_photo
 *
 * The followings are the available model relations:
 * @property Alumni[] $alumnis
 * @property BatchHistory[] $batchHistories
 * @property Donation[] $donations
 * @property Volunteer[] $volunteers
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_code, username, password, roles, user_fname, user_lname, user_mname, user_gender, user_birthdate, user_marital_status, user_address1, user_mobile, user_email', 'required'),
			array('username, password, user_fname, user_lname', 'length', 'max'=>45),
			array('user_photo','file','types'=>'jpg, gif, png, bmp, jpeg,JPG',
                  'maxSize'=>1024 * 1024 * 10, // 10MB
                  'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',
                  'allowEmpty' => true),
			array('roles', 'length', 'max'=>11),
			array('user_mname', 'length', 'max'=>4),
			array('user_gender', 'length', 'max'=>7),
			//array('currentPass,newPass,repNewPass','required'),
			//validator
			array('username', 'unique','message'=>"{attribute} already exists"),
			/*array('password','ext.SPasswordValidator'),*/
			//array('user_fname','ext.alpha',),
			//array('user_lname','ext.alpha',),
			//array('user_mname','ext.alpha',),
			/*array('user_lname,user_fname','match' ,'pattern'=>'/^[A-Za-z_\s\']+$/u','message'=>"{attribute} contains not allowed characters"),
			array('user_mname','match' ,'pattern'=>'/^[A-Za-z_]+$/u','message'=>"{attribute} contains not allowed characters"),
*/			array('user_email','email',),
			
			
			/*array('user_mobile','numerical','integerOnly'=>true,'min'=>12,'max'=>12,'tooSmall'=>'mobile should contain 12 numbers','tooBig'=>'mobile cannot contain more than 12 numbers'),*/
			
			/*array('user_email', 'email','message'=>"Please enter a valid email"),
        	array('user_email', 'unique','message'=>'Email already exists!'), */

        	/*array('username', 'unique','message'=>'username already exists!'),*/
			array('user_marital_status, user_landline', 'length', 'max'=>10),
			array('user_address1, user_address2', 'length', 'max'=>255),
	
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_fname, user_lname, user_mname', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'alumnis' => array(self::HAS_MANY, 'Alumni', 'user_id'),
			'batchHistories' => array(self::HAS_MANY, 'BatchHistory', 'user_id'),
			'donations' => array(self::HAS_MANY, 'Donation', 'user_id'),
			'volunteers' => array(self::HAS_MANY, 'Volunteer', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_code' => 'User Id',
			'username' => 'Username',
			'password' => 'Password',
			'roles' => 'Type',
			'user_fname' => 'Firstname',
			'user_lname' => 'Lastname',
			'user_mname' => 'Middle Initial',
			'user_gender' => 'Gender',
			'user_birthdate' => 'Birthdate',
			'user_marital_status' => 'Marital Status',
			'user_address1' => 'Address1',
			'user_address2' => 'Address2',
			'user_mobile' => 'Mobile',
			'user_landline' => 'Landline',
			'currentPass' => 'Current Password',
			'newPass' => 'New Password',
			'repNewPass' => 'Repeat New Password',
			'user_email' => 'Email',
			'user_photo' => 'Photo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('roles',$this->roles,true);
		$criteria->compare('user_fname',$this->user_fname,true);
		$criteria->compare('user_lname',$this->user_lname,true);
		$criteria->compare('user_mname',$this->user_mname,true);
		$criteria->compare('user_gender',$this->user_gender,true);
		$criteria->compare('user_birthdate',$this->user_birthdate,true);
		$criteria->compare('user_marital_status',$this->user_marital_status,true);
		$criteria->compare('user_address1',$this->user_address1,true);
		$criteria->compare('user_address2',$this->user_address2,true);
		$criteria->compare('user_mobile',$this->user_mobile,true);
		$criteria->compare('user_landline',$this->user_landline,true);
		$criteria->compare('user_email',$this->user_email,true);
		$criteria->compare('user_photo',$this->user_photo,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public $totalVolunteer;
	public $totalAlumni;
	
	public $currentPass;
	public $newPass;
	public $repNewPass;

	public function behaviors()
	{
  	  return array(
        'LoggableBehavior'=>
            'application.modules.auditTrail.behaviors.LoggableBehavior',
   	 );
	}
	
	public $uploadedFile;

	public function getRoles()
	{
		return array('encoder'=>'Encoder','admin'=>'Admin','alumni'=>'Alumni','volunteer'=>'Volunteer');
	}

	public function getVolunteerName()
	{
		return $this->user_fname.' '.$this->user_mname.'. '.$this->user_lname;
	}

	public function getAlumniName()
	{
		return $this->user_fname.' '.$this->user_mname.'. '.$this->user_lname;
	}
	public function getUserCode()
	{
		return $this->user_code.'-'.$this->id;
	}
	public function getFullName()
	{	
		return $this->user_fname . ' ' . $this->user_mname.'. '.$this->user_lname;
	}

	public function getName()
	{	
		return $this->user_lname . ', ' . $this->user_fname.' '.$this->user_mname.'.'; 
	}

	public function getUser()
	{
		return $this->userCode.' '.$this->fullName;
	}

	public function getGenderOptions(){
		return array('male'=>'Male','female'=>'Female');
	}

	public function getMaritalStatus(){
		return array('single'=>'Single','married'=>'Married','widowed'=>'Widowed','separated'=>'Separated','annuled'=>'Annuled');
	}
}